import {
    Context,ProblemModel,BuiltinModel,PermissionError,DomainModel
} from 'hydrooj';


export async function apply(ctx:Context){   
    ctx.on('handler/after/RecordList#get', async (handler) => {
       const user = await DomainModel.getDictUserByDomainId(handler.user._id);
        // handler.response.body.msg="运行中"+Object.entries(user.system)+"___"+user.system.role;
        // let hasSolved = handler.user.hasPerm(BuiltinModel.PERM.PERM_VIEW_RECORD);;
        let hasSolved = true;
        if (handler.args.pid) {
            const problemStatus = await ProblemModel.getStatus(handler.args.domainId, parseInt(handler.args.pid), handler.user._id);
            hasSolved = problemStatus?.status === BuiltinModel.STATUS.STATUS_ACCEPTED;
        }
    // 如果没有通过该题且没有查看记录权限，则抛出权限错误
        if(user.system.uid!=2&&user.system.role!='root'){
            if (!hasSolved||!handler.args.pid) {
                throw new PermissionError(BuiltinModel.PERM.PERM_VIEW_RECORD);
            }
        }
        
    });
}

